Repository navigation
ASoC: SOF: add mutex to protect the dsp_power_state access - #2600
Conversation
kv2019i
left a comment
There was a problem hiding this comment.
@lyakh point on mutex init needs addressing.
Otherwise looks good. I would prefer a bit more explanation in what kind of scenarios there could be many threads accessing. I guess on Intel platforms, this is primarily hda->d0i3_work work queue. Do we have other cases as well?
Same on my side, I don't get the issue. IIRC the transition to D0i3 will be handled by a system suspend with state==S0, and the exit is with any IPC initiated by the driver. @ranj063 probably need you to chime in on this one. |
There could be more than one thread read/write the dsp_power_state simultaneously (e.g. hda_dsp_d0i3_work and sof_ipc_tx_message), add a mutex power_state_access to make sure the access to it is mutually exclusive. Signed-off-by: Keyon Jie <yang.jie@linux.intel.com>
bef9eaa to
7a8a8aa
Compare
|
@kv2019i @plbossart basically if snd_sof_dsp_set_power_state() is called simultaneously, the issue could happen. e.g. I think this is what scenario we hit exactly here: #2571 So, @kv2019i this PR should be enough to fix the issue #2571 theoretically, @keqiaozhang can you help to run stress with this PR also (don't apply #2581)? |
I am pretty sure we discussed this case with @ranj063 and I am not convinced we have such a blatant race condition in the code today. Ranjani, please chime in, my memory is fading... |
@plbossart @ranj063 I don't know what you have discussed about this, but yes, the race condition here is obvious as I mentioned above, no matter which process (workqueue scheduling process or ioctl trigger stop) has higher priority, it can preempt the other one and the _set_power_state() sequence is indeterminate. |
|
@plbossart @ranj063 Can you take a look at this? I believe this is valid as hda_dsp_d0i3_work is running when DSP is active, so it can for sure run concurrently with ioctls (anything that calls sof_ipc_tx_message()). We cancel the workqueue only when going to S3, but that's not the scenario here. |
@keyonjie sorry for the delay. Basically I think the problem we're trying to fix is simultaenous requests from different threads to enter D0i0 and D0i3, right? I dont think we need a mutex for setting the power state at the top-level. I'd think that cancelling any pending D0i3 work before setting D0i0 in hda_dsp_set_power_state (), specifically hda_dsp_set_D0_state() should be enough. What do you think? |
|
@ranj063 wrote:
One of the threads is ipc_tx_message() which can be called at quite high frequency. Taking a mutex pretty lightweight operation compared to a mandatory sync point. It of course depends on high likely it is to have the D0i3 work queue running when sending a lot of IPCs, but this seems like a valid case. |
@kv2019i it's more a question of whether we need it at the system level or not. The core is agnostic to the dsp substates. So a race in setting the substates should be handled at the platform level IMHO |
@ranj063 no, we can't cancel pending D0i3 work (name it A below) in hda_dsp_set_power_state/hda_dsp_set_D0_state() (name it B below). Basically, B is a platform specific callback sof_ops(sdev)->set_power_state here, it is some kind of helpers. A here is actually a caller to B, we should not cancel it's caller from B internal. Here, the A is a caller to B, and the ipc_tx_message() is another caller to B, there could be more callers to B added in future, adding mutex to make sure B is used/called exclusively looks naturally and low cost to me. @ranj063 I don't think you should concern about the top-level mutex: the dsp_power_state is a snd_sof_dev top level member, so the mutex protection should naturally be at that level also. And for those platforms which doesn't support dsp_power_state (the sof_ops(sdev)->set_power_state is NULL), only a pare of mutex_lock/_unlock() calling should be light enough also. |
|
Hmm, @ranj063 it's indeed a bit problematic to force locking in core, if the reason to lock is platform specific. OTOH, we do have "dsp_power_state" in common code, so based on kernel practises, the mutex should be there as well. We could push locking to hw-dependent code, but as common code does touch the data as well (e.g. in sof_ipc_tx_message()), this wouldn't work. So it does look that the ability to lock is something core should be providing to different hw backends, and both common core code and specific implementations should take the lock whenever changing DSP state. Zooming back a bit, it still makes sense. As core is using the data in code that handles user-initiated actions (ioctls essentially), data in question is related to PM state and PM flows are typically initiated asynchronously, this capability might be needed for other hw as well. In other words, I don't think this is very specific to Intel hardware. |
|
In principle LGTM now - thanks for fixing @keyonjie ! But I'm thinking about the following: the added mutex protects
Some are trivial (probing) and need no protection, but others are less trivial and since we're adding a generic protection for this field, we need to at least explain why we don't need to protect it everywhere? I.e. add comments in D0i3 work (it's deactivated when suspended) and in resume (cannot yet race with anything when resuming)? |
Thanks for went through details during the reviewing @lyakh . For probing, yes, we are doing initialization only, we add wrap to use a function like snd_sof_dsp_power_state_init(), but not necessary IMO. As we are in suspended state here nobody else (except the resume callback itself) has chance to touch the dsp_power_state, so we are still good here. But yes, maybe I can add some comment here. |
|
Note to all reviewers: tagging as P1 to expedite the review. |
|
SOFCI TEST |
There could be more than one thread read/write the dsp_power_state
simultaneously, add a mutex power_state_access to make sure the access
to it is mutually exclusive.
Signed-off-by: Keyon Jie yang.jie@linux.intel.com